/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.sd.jxta.advertisements;

import co.edu.sd.jxta.service.ServiceType;
import net.jxta.document.AdvertisementFactory;
import net.jxta.peergroup.PeerGroup;

import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;

import net.jxta.id.IDFactory;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeID;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author SD
 */
public class AdvertisementsMSPFactory {

    public static PipeAdvertisement getTestAdvertisement(PeerGroup netPeerGroup) {
        PipeAdvertisement advertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
        advertisement.setPipeID(IDFactory.newPipeID(PeerGroupID.defaultNetPeerGroupID));
        advertisement.setType(PipeService.UnicastType);
        advertisement.setName("Discovery tutorial");
        return advertisement;
    }

    public static PipeAdvertisement getFileUploadAdvertisement() {
        return createAdvertisement(ServiceType.FILE_UPLOAD);
    }

    public static PipeAdvertisement getAppExecAdvertisement() {
        return createAdvertisement(ServiceType.APP_EXEC);
    }

    public static PipeAdvertisement getFileDownloadAdvertisement() {
        return createAdvertisement(ServiceType.FILE_DOWNLOAD);
    }

    public static PipeAdvertisement getUTCTimeAdvertisement() {
        return createAdvertisement(ServiceType.UTC_TIME);
    }

    public static PipeAdvertisement getFileSearchAdvertisement() {
        return createAdvertisement(ServiceType.FILE_SEARCH);
    }

    /**
     * 
     * @param st
     * @return
     */
    public static PipeAdvertisement getAdvertisementByServiceType(ServiceType st) {
        return createAdvertisement(st);
    }

    public static PipeID createPipeID(PeerGroupID pgID, String pipeName) {
        String seed = pipeName + SEED;
        return IDFactory.newPipeID(pgID, hash(seed.toLowerCase()));
    }

    public static PeerGroupID createInfraPeerGroupID(String groupName) {
        return createPeerGroupID(groupName);
    }
    public static PeerGroupID createPeerGroupID(final String groupName) {
        // Use lower case to avoid any locale conversion inconsistencies
        return IDFactory.newPeerGroupID(PeerGroupID.defaultNetPeerGroupID, hash(SEED + groupName.toLowerCase()));
    }

     public static PeerID createPeerID(PeerGroupID pgID, String peerName) {
        String seed = peerName + SEED;
        return IDFactory.newPeerID(pgID, hash(seed.toLowerCase()));
    }

    /**
     * 
     * @param netPeerGroup
     * @param st
     * @return
     */
    private static PipeAdvertisement createAdvertisement(ServiceType st) {
        PipeAdvertisement advertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
        PipeID pipeID = createPipeID(PeerGroupID.defaultNetPeerGroupID, "pipe"+st.getName());

        System.out.println("--creating adv pipeID " + pipeID);
        advertisement.setPipeID(pipeID);
        advertisement.setType(PipeService.UnicastType);
        advertisement.setName(st.getName());
        advertisement.setDescription(st.getDesc());
        return advertisement;


        /**
         PipeAdvertisement advertisement = (PipeAdvertisement)
                AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());

        advertisement.setPipeID(socketID);
        advertisement.setType(PipeService.UnicastType);
        advertisement.setName("Socket tutorial");
        return advertisement;
         */
    }
   
    private static final String SEED = "IDTutorial" + System.currentTimeMillis();

    private static byte[] hash(final String expression) {
        byte[] result;
        MessageDigest digest;

        if (expression == null) {
            throw new IllegalArgumentException("Invalid null expression");
        }

        try {
            digest = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException failed) {
            failed.printStackTrace(System.err);
            RuntimeException failure = new IllegalStateException("Could not get SHA-1 Message");
            failure.initCause(failed);
            throw failure;
        }

        try {
            byte[] expressionBytes = expression.getBytes("UTF-8");
            result = digest.digest(expressionBytes);
        } catch (UnsupportedEncodingException impossible) {
            RuntimeException failure = new IllegalStateException("Could not encode expression as UTF8");

            failure.initCause(impossible);
            throw failure;
        }
        return result;
    }
}
